package com.ljyhhh.SmartCity.activity.home

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.ljyhhh.SmartCity.R
import com.ljyhhh.SmartCity.activity.listView.ActivityNetRequestListView
import kotlinx.android.synthetic.main.activity_home.*
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import okio.IOException
import org.json.JSONObject


class MineActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        NetRequestList.setOnClickListener {
            var intent=Intent(this, ActivityNetRequestListView::class.java)
            startActivity(intent)
        }
        NetPostJson.setOnClickListener {
            asyncPost()

        }
    }
    private fun asyncPost() {
        val url = "http://8.137.8.86/api/student/login"
        var jsonObject = JSONObject()
        jsonObject.put("studentNumber","1713630001")
        jsonObject.put("password","123456")
        val jsonBody=jsonObject.toString().toRequestBody("application/json".toMediaType())
        //添加post请求参数
        val requestBody = FormBody.Builder()
            .add("studentNumber","1713630001")
            .add("password","123456")
            .build()

        //创建request请求对象
        val request = Request.Builder()
            .url(url)
            .post(requestBody)
            .build()

        //创建call并调用enqueue()方法实现网络请求
        OkHttpClient().newCall(request)
            .enqueue(object : Callback {
                override fun onFailure(call: Call, e: IOException) {
                }

                override fun onResponse(call: Call, response: Response) {
                    response.body?.string()?.let { Log.d("resp", it) }
                }
            })
    }

}